import { _decorator, Button, director, Label } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { GameMRTZ_TZ_Ctr } from './GameMRTZ_TZ_Ctr';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
import { GameHD5_Ctr } from '../HD/GameHD5_Ctr';
import { DALIYCHAType, Respones } from '../../Config/GameCfg';
import { GameTip } from '../../../../Framework/Scripts/Managers/TipMgr';
import { NativeMgr } from '../../../../Framework/Scripts/Engine/NativeMgr';
import { DailyChallengeCfgRes, DailyChallengeRes, GameTzAddCountRes } from '../../../AssetPackage/Props/Hall/Game/GameTZ';
import { IDailyChallengeCfgRes, IDailyChallengeRes, IDcAddCountRes } from '../GamHall/GameHallProto';
const { ccclass, property } = _decorator;

export let TZType = {
    Type_XX: 1, //休闲
    Type_DS: 2, //大神
}

/**
 * 每日挑战
 */
@ccclass('GameMRTZ_Ctr')
export class GameMRTZ_Ctr extends UIComponent {
    labQuan: Label;     //抽奖券
    labResidue: Label;  //剩余挑战次数

    isClick: boolean;
    labCostXX: Label;
    labCostDS: Label;
    labShowView: Label;
    btnView: Button;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
        this.setBgAni(0);
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.labQuan = this.ViewComponent('spBg/Label',Label);
        this.labResidue = this.ViewComponent('labResidue',Label);
        this.labCostXX = this.ViewComponent('spBg1/Label',Label);
        this.labCostDS = this.ViewComponent('spBg2/Label',Label);
        this.labShowView = this.ViewComponent('layout/btnResidue',Label);

        this.AddButtonListener('btnBack',this,this.onBtnCloseClick1);
        this.AddButtonListener('spBg1',this,this.onBtnXXClick);
        this.AddButtonListener('spBg2',this,this.onBtnDSClick);
        this.AddButtonListener('spBg3',this,this.onBtnCJClick);
        this.AddButtonListener('layout/btnResidue',this,this.onBtnSeeView);
    
        this.btnView = this.ViewComponent('layout/btnResidue',Button);
    }

    private InitData(){
        this.isClick = true;
        globalThis.gameMgr._GameHallMsg.DailyChallengeCfgReq();
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgDailyChallengeCfgRes,this.onmsgDailyChallengeCfgRes,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgDailyChallengeRes,this.onDailyChallengeRes,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGameTzAddCountRes,this.onGameTzAddCountRes,this);
        
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgDailyChallengeCfgRes,this.onmsgDailyChallengeCfgRes,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgDailyChallengeRes,this.onDailyChallengeRes,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgGameTzAddCountRes,this.onGameTzAddCountRes,this);
    }

    private onBtnCloseClick1(){
        SceneMgr.inst.showHall();
    }

    private setTzCountDes(count: number){
        this.labResidue.string = `今日剩余挑战次数：${count}次`;
    }

    private setViewDes(des: string,line: string){
        this.labShowView.string = des;

        this.labShowView.node.children[0].getComponent(Label).string = line;
    }

    //isClick == false禁用 true 启用
    private setBntViewStatus(isClick:boolean){
        this.btnView.interactable = isClick;
    }

    private onBtnXXClick(btn: Button){
        console.log('===========点击休闲挑战==========');
        if(this.isClick === false){
            return;
        }
        this.isClick = false;
        let type = DALIYCHAType.DC_XX;
        globalThis.gameMgr._GameHallMsg.DailyChallengeReq(type);
    }

    private onBtnDSClick(btn: Button){
        if(this.isClick === false){
            return;
        }
        this.isClick = false;
        console.log('===========点击大神挑战==========');
        let type = DALIYCHAType.DC_DS;
        globalThis.gameMgr._GameHallMsg.DailyChallengeReq(type);
    }

    private onBtnCJClick(btn: Button){
        let com = UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/HD/GameHD5',UIType.Full) as GameHD5_Ctr;
        if(com){
            com.setCloseView(AbNames.GameHall,'Prefabs/GameMRTZ/GameMRTZ');
        }
    }

    //观看视屏获得挑战机会
    private onBtnSeeView(btn: Button){
         //这里注意，是看完视频的回调才发送消息处理，目前没有广告，先通消息
        NativeMgr.inst.seeView(()=>{
            globalThis.gameMgr._GameHallMsg.GameTzAddCountReq();
        });
    }

    //每日挑战配置返回
    private onmsgDailyChallengeCfgRes(data:IDailyChallengeCfgRes){
        console.log('==========onmsgDailyChallengeCfgRes=============');
        let code = data.code || 0;
      
        if(code === Respones.OK){
            if(this.hasProInObj(data,'count')){
                this.setTzCountDes(data.count);
            }
            if(this.hasProInObj(data,'ticket')){
                this.labQuan .string = `${data.ticket}`;
            }
            if(this,this.hasProInObj(data,'costXX')){
                if(data.costXX == 0){
                    this.labCostXX.string = "免费入场";
                }
            }
            if(this.hasProInObj(data,'costDS')){
                this.labCostDS.string = `消耗${data.costDS}球币入场`;
            }

            if(this.hasProInObj(data,'status')){
                if(data.status == 2){ //广告次数使用完毕
                    let line = '                                         ';
                    this.setViewDes('广告正在准备中!',line);
                    this.setBntViewStatus(false);
                }else if(data.status == 1){
                    let line = '                                                         ';
                    this.setViewDes('观看视频获得挑战机会',line);
                    this.setBntViewStatus(true);
                }
            }
        }
    }

    //选则配置返回
    private onDailyChallengeRes(data: IDailyChallengeRes){
        console.log('==========onDailyChallengeRes=============',data);
        this.isClick = true;
        let com = UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameMRTZ/GameMRTZ_TZ',UIType.Full) as GameMRTZ_TZ_Ctr;
        if(com){
            com.setData(data);
        }
    }

    //看视屏添加配置返回
    private onGameTzAddCountRes(data: IDcAddCountRes){
        console.log('==========onGameTzAddCountRes===========');

        if(this.hasProInObj(data,'code')){
         
            if(data.code === Respones.OK){
                this.setTzCountDes(data.count);
       
                if(data.status == 2){ //广告次数使用完毕
                    GameTip.inst.showTip('今日看视频使用完毕！');
                    let line = '                                         ';
                    this.setViewDes('广告正在准备中!',line);
                    this.setBntViewStatus(false);
                }
            }else{
                let line = '                                         ';
                this.setViewDes('广告正在准备中!',line);
            }
        }
    }
}


